package util;

import javax.servlet.http.HttpServletRequest;

public class MiscUtil {

	public static int getIntUrlParam(HttpServletRequest request,
			String paramName) {
		int id = getIntUrlParam(request, paramName, Integer.MIN_VALUE);
		return id;
	}

	public static int getIntUrlParam(HttpServletRequest request,
			String paramName, int defaultValue) {
		int id = defaultValue;
		if (request.getParameter(paramName) != null
				&& !request.getParameter(paramName).isEmpty()) {
			try {
				id = Integer.parseInt(request.getParameter(paramName));
			} catch (Exception e) {
			}
		}
		return id;
	}

	public static boolean getBoolUrlParam(HttpServletRequest request,
			String paramName) {
		boolean val = getBoolUrlParam(request, paramName, false);
		return val;
	}

	public static boolean getBoolUrlParam(HttpServletRequest request,
			String paramName, boolean defaultValue) {
		boolean val = defaultValue;
		if (request.getParameter(paramName) != null
				&& !request.getParameter(paramName).isEmpty()) {
			if (request.getParameter(paramName).equalsIgnoreCase("true")
					|| request.getParameter(paramName).equalsIgnoreCase("1")) {
				val = true;
			} else if (request.getParameter(paramName)
					.equalsIgnoreCase("false")
					|| request.getParameter(paramName).equalsIgnoreCase("0")) {
				val = false;
			}
		}
		return val;
	}

	public static String getStringUrlParam(HttpServletRequest request,
			String paramName) {
		String val = "";
		if (request.getParameter(paramName) != null
				&& !request.getParameter(paramName).isEmpty()) {
			val = request.getParameter(paramName);
		}
		return val;
	}

	// public static String getStringUrlParam(HttpServletRequest request, String
	// paramName, String defaultValue) {
	// String val = defaultValue;
	// if (request.getParameter(paramName) != null &&
	// !request.getParameter(paramName).isEmpty()) {
	// val = request.getParameter(paramName);
	// }
	// return val;
	// }

	public static boolean compareByteArray(byte[] src, byte[] target) {
		if (src != null && target == null) {
			return false;
		}
		if (src == null && target != null) {
			return false;
		}
		if (src.length != target.length) {
			return false;
		}

		for (int i = 0; i < src.length; i++) {
			byte srcByte = src[i];
			byte targetcByte = target[i];
			if (srcByte != targetcByte) {
				return false;
			}
		}

		return true;
	}

}
